package java基础;

import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class CreateThread {
    //基于继承Thread类而创建线程；
    class MyThread extends Thread{
        @Override
        public void run() {
            System.out.println("线程启动了");
        }
    }
    @Test
    public void test1(){
        MyThread myThread = new MyThread();
        myThread.start();
    }
    //基于实现Runnable接口而创建线程；
    class MyRunnable implements Runnable{
        @Override
        public void run() {
            System.out.println("线程启动了----基于实现Runnable接口而创建线程；");
        }
    }
    @Test
    public void test2(){
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start();
    }
    //基于callable接口而创建线程；
    class MyCallable implements Callable {
        @Override
        public Object call() throws Exception {//call方法返回值
            System.out.println("线程启动了----基于callable接口而创建线程；");
            return Thread.currentThread().getId();
        }
    }
    @Test
    public void test3() throws Exception {
        MyCallable myCallable = new MyCallable();
        FutureTask futureTask = new FutureTask(myCallable);//创建FutureTask对象
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println(futureTask.get());

    }
    //基于线程池而创建线程；
    class MyRunnable2 implements Runnable{
        @Override
        public void run() {
            System.out.println("线程启动了----基于线程池而创建线程；");
        }
    }
    @Test
    public void test4(){
        //基于线程池而创建线程；

    }


}
